<?php
namespace App\Services\Fund\FundAccount;

use App\Models\Fund\CurrencyAccount;
use App\Models\Fund\FundAccount;
use App\Models\Fund\IntegralAccount;
use App\Models\Fund\FundUnfreezeLog;
use App\Services\Fund\ValidateFundAccount;
use App\Models\Fund\FundFreezeLog;
use Illuminate\Support\Facades\DB;
use App\Models\Fund\OperateReason;
use App\Models\Fund\FundOptionLog;
use App\Services\User\User;

class FundAccountLogic
{
    public $scale = 1;


    /*值为money的亚数币/积分 转换为多少余额*/
    public function exchange($fundClass, $data): array
    {
        if($data['money'] == 0){
            return auto_success(0);
        }
        $this->_updateScale($data);
        //$res = bcmul( bcdiv($fundClass->scale, $this->scale, DECIMALS), $data['money'] );
        $res = bcdiv(bcmul($fundClass->scale, $data['money']), $this->scale, DECIMALS);
        return auto_return($res);
    }

    //修改比例属性值
    private function _updateScale($data)
    {
        $config = config('tpproject.fundScale');
        $this->scale = $config[$data['app_id']]['Fund'];
    }

//    // 初始化资金账户
//    public function init(array $data): array
//    {
//        $saveAccount['user_id'] = $data['user_id'];
//        $saveAccount['create_ip'] = $data['create_ip'];
//        $saveAccount['total'] = 0;
//        $saveAccount['income'] = 0;
//        $saveAccount['expend'] = 0;
//        $saveAccount['cash'] = 0;
//        $saveAccount['freeze'] = 0;
//        $saveAccount['update_ip'] = '';
//
//        $saveLog['user_id'] = $data['user_id'];
//        $saveLog['opposite_id'] = SYSTEM_ID;
//        $saveLog['app_id'] = $data['app_id'];
//        $saveLog['type'] = 'init';
//        $saveLog['money'] = 0;
//        $saveLog['total'] = 0;
//        $saveLog['income'] = 0;
//        $saveLog['expend'] = 0;
//        $saveLog['cash'] = 0;
//        $saveLog['freeze'] = 0;
//        $saveLog['remark'] = '用户[' . $data['user_id'] . ']资金初始化';
//        $saveLog['create_ip'] = $data['create_ip'];
//        $saveLog['update_ip'] = '';
//        $saveLog['status'] = 1;
//        $saveLog['user_info'] = '{}';
//        $saveLog['opposite_info'] = '{}';
//
//        // 启动事务
//        $this->db()->startTrans();
//        try{
//            $resAccount = $this->model($this->fundAccountModel)->save($saveAccount);
//            $resLog = $this->model($this->fundOptionLogModel)->save($saveLog);
//            // 提交事务
//            $this->db()->commit();
//        } catch (\Exception $e) {
//            // 回滚事务
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//
//        return auto_return($resAccount, '成功初始化资金账户');
//    }
    # 充值 系统->a
    public function prepaid(array $data): array
    {
        $whereUpdateUserAccount['user_id'] = $data['user_id'];
        $whereUpdateSystemAccount['user_id'] = SYSTEM_ID;
        // 用户资金日志(部分)
        $userInfo = json_decode($data['user_info'], true);
        $userAction = isset($userInfo['name']) ? $userInfo['name'] : $data['user_id'];
        $remark = (isset($data['remark']) && $data['remark']) ? '为用户[' . $userAction . ']充值：充值原因：' . $data['remark'] : '为用户[' . $userAction . ']充值';
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = SYSTEM_ID;
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'prepaid';
        $saveUserLog['money'] = $data['money'];
        $saveUserLog['remark'] = $remark;
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = $data['user_info'];
        $saveUserLog['opposite_info'] = $data['opposite_info'];
        // 系统资金日志(部分)
        $saveSystemLog['user_id'] = SYSTEM_ID;
        $saveSystemLog['opposite_id'] = $data['user_id'];
        $saveSystemLog['app_id'] = $data['app_id'];
        $saveSystemLog['type'] = 'prepaid';
        $saveSystemLog['money'] = -$data['money'];
        $saveSystemLog['remark'] = $remark;
        $saveSystemLog['create_ip'] = $data['update_ip'];
        $saveSystemLog['update_ip'] = $data['update_ip'];
        $saveSystemLog['status'] = 1;
        $saveSystemLog['user_info'] = $data['user_info'];
        $saveSystemLog['opposite_info'] = $data['opposite_info'];


        // 启动事务
        DB::beginTransaction();
        try{
            // 更改用户资金
            $resUserAccount = DB::update("update `tp_fund_account` set `update_ip` = ?, `total` = (total+?), `income` = (income+?), `cash` = (cash+?), `update_time` = ? where `user_id` = ?",[$data['update_ip'],$data['money'],$data['money'],$data['money'],NOW,$data['user_id']]);
            // 更改系统资金
            $resUserAccount = DB::update("update `tp_fund_account` set `update_ip` = ?, `total` = (total-?), `expend` = (expend+?), `cash` = (cash-?), `update_time` = ? where `user_id` = ?",[$data['update_ip'],$data['money'],$data['money'],$data['money'],NOW,SYSTEM_ID]);
            // 查询当前用户和系统的资金状态
//            DB::listen(function($query) {
//                $bindings = $query->bindings;
//                $sql = $query->sql;
//                foreach ($bindings as $replace){
//                    $value = is_numeric($replace) ? $replace : "'".$replace."'";
//                    $sql = preg_replace('/\?/', $value, $sql, 1);
//                }
//                dd($sql);
//            });
            $infoUserAccount = FundAccount::SharedLock()->where($whereUpdateUserAccount)->first()->toArray();
            // 查询当前系统的资金状态
            $infoSystemAccount = FundAccount::SharedLock()->where($whereUpdateSystemAccount)->first()->toArray();
            // 补全用户资金日志
            $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
            // 补opposite资金日志
            $saveSystemLog = $this->_replenishFundLogInfo($saveSystemLog, $infoSystemAccount);
            // 写入资金日志
            $res = $this->_saveFundLogInfo($saveSystemLog, $saveUserLog);
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }

        return auto_return($res, '充值成功');
    }
//    # 转账 a->b
   public function transfer(array $data): array
   {
       // 更改用户资金
       $whereUpdateUserAccount['user_id'] = $data['user_id'];
       $whereUpdateUserAccount['cash'] = ['EGT', $data['money']];
       $updateUserAccount['update_ip'] = $data['update_ip'];
       $updateUserAccount['update_time'] = NOW;
       // 更改对方资金
       $whereUpdateOppositeAccount['user_id'] = $data['opposite_id'];
       $updateOppositeAccount['update_ip'] = $data['update_ip'];
       $updateOppositeAccount['total'] = ['exp', 'total+' . $data['money']];
       $updateOppositeAccount['income'] = ['exp', 'income+' . $data['money']];
       $updateOppositeAccount['cash'] = ['exp', 'cash+' . $data['money']];
       $updateOppositeAccount['update_time'] = NOW;
       // 用户资金日志(部分)
       $userInfo = json_decode($data['user_info'], true);
       $oppositeInfo = $data['opposite_info'];
       $saveUserLog['user_id'] = $data['user_id'];
       $saveUserLog['opposite_id'] = $data['opposite_id'];
       $saveUserLog['app_id'] = $data['app_id'];
       $saveUserLog['type'] = 'transfer';
       $saveUserLog['money'] = -$data['money'];
       $saveUserLog['remark'] = isset($oppositeInfo['name']) ? '转账给用户[' . $oppositeInfo['name'] . ']资金' : '转账给用户[' . $data['opposite_id'] . ']资金';
       $saveUserLog['create_ip'] = $data['update_ip'];
       $saveUserLog['update_ip'] = $data['update_ip'];
       $saveUserLog['status'] = 1;
       $saveUserLog['user_info'] = $data['user_info'];
       $saveUserLog['opposite_info'] = $data['opposite_info'];
       // opposite用户资金日志(部分)
       $saveOppositeLog['user_id'] = $data['opposite_id'];
       $saveOppositeLog['opposite_id'] = $data['user_id'];
       $saveOppositeLog['app_id'] = $data['app_id'];
       $saveOppositeLog['type'] = 'transfer';
       $saveOppositeLog['money'] = $data['money'];
       $saveOppositeLog['remark'] = isset($userInfo['name']) ? '用户[' . $userInfo['name'] . ']转账给我资金' : '用户[' . $data['user_id'] . ']转账给我资金';
       $saveOppositeLog['create_ip'] = $data['update_ip'];
       $saveOppositeLog['update_ip'] = $data['update_ip'];
       $saveOppositeLog['status'] = 1;
       $saveOppositeLog['user_info'] = $data['user_info'];
       $saveOppositeLog['opposite_info'] = $data['opposite_info'];

       // 启动事务
       DB::beginTransaction();
       try{
           // 检查账户 a的可用金额是否充足
           $findInfoUserAccount = FundAccount::SharedLock()->where([['user_id','=',$data['user_id']],['cash','>=',$data['money']]])->first();
           if(is_null($findInfoUserAccount)){ return \verify_err(null, '转出用户不存在或可用资金不足'); }
           // 扣减a 帐户可用金额
            $resUpdateUserAccount = DB::update("update `tp_fund_account` set `update_ip` = ?, `total` = (total-?), `expend` = (expend+?),`cash` = (cash-?), `update_time` = ? where `user_id` = ? and `cash` >= ?",[$data['update_ip'],$data['money'],$data['money'],$data['money'],NOW,$data['user_id'],$data['money']]);
           // 增加b 帐户可用金额
           $resUpdateOppositeAccount = DB::update("update `tp_fund_account` set `update_ip` = ?, `total` = (total+?), `income` = (income+?),`cash` = (cash+?), `update_time` = ? where `user_id` = ?",[$data['update_ip'],$data['money'],$data['money'],$data['money'],NOW,$data['opposite_id']]);
           // 查询当前用户的资金状态
           $infoUserAccount = FundAccount::SharedLock()->where('user_id','=',$data['user_id'])->first();
           // 查询当前收款账户的资金状态
           $infoOppositeAccount = FundAccount::SharedLock()->where('user_id','=',$data['opposite_id'])->first();
           // 补全用户资金日志
           $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
           // 补opposite资金日志
           $saveOppositeLog = $this->_replenishFundLogInfo($saveOppositeLog, $infoOppositeAccount);
           // 写入资金日志
           $this->_saveFundLogInfo($saveOppositeLog, $saveUserLog);

           // 提交事务
            DB::commit();
       } catch (\Exception $e) {
           // 回滚事务
           DB::rollback();
           return server_err(null, '数据库异常:' . $e->getMessage());
       }
       return auto_return($infoUserAccount);
   }
//    # 付款 a->b 暂时废弃
//    public function payment(array $data): array
//    {
//        return auto_return($data);
//    }
//    # 借款 a借b的
//    public function borrow(array $data): array
//    {
//        // 更改被借款用户资金
//        $whereUpdateOpoosite['user_id'] = $data['opposite_id'];
//        $whereUpdateOpoosite['cash'] = ['EGT', $data['money']];
//        // 借款日志数据
//        $borrowLog['user_id'] = $data['user_id'];
//        $borrowLog['opposite_id'] = $data['opposite_id'];
//        $borrowLog['money'] = $data['money'];
//        $borrowLog['fund_log_id'] = 0;
//        $borrowLog['refund_money'] = 0;
//        $borrowLog['refund_status'] = 0;
//        $borrowLog['create_ip'] = $data['update_ip'];
//        $borrowLog['update_ip'] = $data['update_ip'];
//        $borrowLog['status'] = 1;
//        $borrowLog['user_info'] = $data['user_info'];
//        $borrowLog['opposite_info'] = $data['opposite_info'];
//        $borrowLog['user_info'] = $data['user_info'];
//        $borrowLog['opposite_info'] = $data['opposite_info'];
//
//        // 减少对方(出借方)资金
//        $whereUpdateOppositeAccount['user_id'] = $data['opposite_id'];
//        $whereUpdateOppositeAccount['cash'] = ['EGT', $data['money']];
//        $updateOppositeAccount['update_ip'] = $data['update_ip'];
//        $updateOppositeAccount['total'] = ['exp', 'total-' . $data['money']];
//        $updateOppositeAccount['expend'] = ['exp', 'expend+' . $data['money']];
//        $updateOppositeAccount['cash'] = ['exp', 'cash-' . $data['money']];
//        // 更改借款用户资金
//        $whereUpdateUserAccount['user_id'] = $data['user_id'];
//        $updateUserAccount['update_ip'] = $data['update_ip'];
//        $updateUserAccount['total'] = ['exp', 'total+' . $data['money']];
//        $updateUserAccount['income'] = ['exp', 'income+' . $data['money']];
//        $updateUserAccount['cash'] = ['exp', 'cash+' . $data['money']];
//
//        // 用户资金日志(部分)
//        $userInfo = json_decode($data['user_info'], true);
//        $oppositeInfo = json_decode($data['opposite_info'], true);
//        $saveUserLog['user_id'] = $data['user_id'];
//        $saveUserLog['opposite_id'] = $data['opposite_id'];
//        $saveUserLog['app_id'] = $data['app_id'];
//        $saveUserLog['type'] = 'borrow';
//        $saveUserLog['money'] = $data['money'];
//        $saveUserLog['remark'] = isset($oppositeInfo['name']) ? '从用户[' . $oppositeInfo['name'] . ']借款' : '从用户[' . $data['opposite_id'] . ']借款';
//        $saveUserLog['create_ip'] = $data['update_ip'];
//        $saveUserLog['update_ip'] = $data['update_ip'];
//        $saveUserLog['status'] = 1;
//        $saveUserLog['user_info'] = $data['user_info'];
//        $saveUserLog['opposite_info'] = $data['opposite_info'];
//
//        // opposite用户资金日志(部分)
//        $saveOppositeLog['user_id'] = $data['opposite_id'];
//        $saveOppositeLog['opposite_id'] = $data['user_id'];
//        $saveOppositeLog['app_id'] = $data['app_id'];
//        $saveOppositeLog['type'] = 'borrow';
//        $saveOppositeLog['money'] = -$data['money'];
//        $saveOppositeLog['remark'] = isset($userInfo['name']) ? '借给用户[' . $userInfo['name'] . ']资金' : '借给用户[' . $data['user_id'] . ']资金';
//        $saveOppositeLog['create_ip'] = $data['update_ip'];
//        $saveOppositeLog['update_ip'] = $data['update_ip'];
//        $saveOppositeLog['status'] = 1;
//        $saveOppositeLog['user_info'] = $data['user_info'];
//        $saveOppositeLog['opposite_info'] = $data['opposite_info'];
//        // 启动事务
//        $this->db()->startTrans();
//        try{
//            // 检查账户 a的可用金额是否充足
//            $findInfoUserAccount = $this->model($this->fundAccountModel)->lock(true)->where($whereUpdateOpoosite)->find();
//            if(is_null($findInfoUserAccount)){ return \verify_err(null, '转出用户不存在或可用资金不足'); }
//            // 写入借款日志
//            $this->model($this->fundBorrowLogModel)->save($borrowLog);
//            $borrowLogId = $this->model($this->fundBorrowLogModel)->getLastInsID();
//            // 减少b的
//            $resUpdateOppositeAccount = $this->model($this->fundAccountModel)->save($updateOppositeAccount, $whereUpdateOppositeAccount);
//            // 增加a的
//            $resUpdateUserAccount = $this->model($this->fundAccountModel)->save($updateUserAccount, $whereUpdateUserAccount);
//            // 查询当前用户的资金状态
//            $infoUserAccount = $this->model($this->fundAccountModel)->lock(true)->where(['user_id'=>$data['user_id']])->find();
//            // 查询当前收款账户的资金状态
//            $infoOppositeAccount = $this->model($this->fundAccountModel)->lock(true)->where($whereUpdateOppositeAccount)->find();
//            // 补全用户资金日志
//            $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
//            // 补opposite资金日志
//            $saveOppositeLog = $this->_replenishFundLogInfo($saveOppositeLog, $infoOppositeAccount);
//            // 写入资金日志
//            $fundLogId = $this->_saveFundLogInfo($saveOppositeLog, $saveUserLog);
//
//            $this->model($this->fundBorrowLogModel)->save(['fund_log_id'=>$fundLogId], ['id'=>$borrowLogId]);
//            // 提交事务
//            $this->db()->commit();
//        } catch (\Exception $e) {
//            // 回滚事务
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//        return auto_return($infoUserAccount);
//    }
//    # 还款 a还b的
//    public function refund(array $data): array
//    {
//        // 还款金额是否合理 <= 欠款金额 还款人金额是否充足
//        $whereFindUser['user_id'] = $data['user_id'];
//        $whereFindUser['cash'] = ['EGT', $data['money']];
//        // 还款人的id 债主的id 等欠款记录信息是否对应
//        $whereFindBorrowLog['id'] = $data['borrow_log_id'];
//        $whereFindBorrowLog['user_id'] = $data['user_id'];
//        $whereFindBorrowLog['opposite_id'] = $data['opposite_id'];
//        $whereFindBorrowLog['refund_status'] = '0';
//
//        // 减少借款用户资金
//        $whereUpdateUserAccount['user_id'] = $data['user_id'];
//        $whereUpdateUserAccount['cash'] = ['EGT', $data['money']];
//        $updateUserAccount['update_ip'] = $data['update_ip'];
//        $updateUserAccount['total'] = ['exp', 'total-' . $data['money']];
//        $updateUserAccount['expend'] = ['exp', 'expend+' . $data['money']];
//        $updateUserAccount['cash'] = ['exp', 'cash-' . $data['money']];
//        // 增加收欠款用户资金
//        $whereUpdateOppositeAccount['user_id'] = $data['opposite_id'];
//        $updateOppositeAccount['update_ip'] = $data['update_ip'];
//        $updateOppositeAccount['total'] = ['exp', 'total+' . $data['money']];
//        $updateOppositeAccount['income'] = ['exp', 'income+' . $data['money']];
//        $updateOppositeAccount['cash'] = ['exp', 'cash+' . $data['money']];
//        // 用户资金日志(部分)
//        $userInfo = json_decode($data['user_info'], true);
//        $oppositeInfo = json_decode($data['opposite_info'], true);
//        $saveUserLog['user_id'] = $data['user_id'];
//        $saveUserLog['opposite_id'] = $data['opposite_id'];
//        $saveUserLog['app_id'] = $data['app_id'];
//        $saveUserLog['type'] = 'refund';
//        $saveUserLog['money'] = -$data['money'];
//        $saveUserLog['remark'] = isset($oppositeInfo['name']) ? '还给用户[' . $oppositeInfo['name'] . ']欠款' : '还给用户[' . $data['opposite_id'] . ']欠款';
//        $saveUserLog['create_ip'] = $data['update_ip'];
//        $saveUserLog['update_ip'] = $data['update_ip'];
//        $saveUserLog['status'] = 1;
//        $saveUserLog['user_info'] = $data['user_info'];
//        $saveUserLog['opposite_info'] = $data['opposite_info'];
//        // opposite用户资金日志(部分)
//        $saveOppositeLog['user_id'] = $data['opposite_id'];
//        $saveOppositeLog['opposite_id'] = $data['user_id'];
//        $saveOppositeLog['app_id'] = $data['app_id'];
//        $saveOppositeLog['type'] = 'refund';
//        $saveOppositeLog['money'] = $data['money'];
//        $saveOppositeLog['remark'] = isset($userInfo['name']) ? '收到用户[' . $userInfo['name'] . ']还款' : '收到用户[' . $data['user_id'] . ']还款';
//        $saveOppositeLog['create_ip'] = $data['update_ip'];
//        $saveOppositeLog['update_ip'] = $data['update_ip'];
//        $saveOppositeLog['status'] = 1;
//        $saveOppositeLog['user_info'] = $data['user_info'];
//        $saveOppositeLog['opposite_info'] = $data['opposite_info'];
//        // 增加一条还款日志
//        $refundLog['user_id'] = $data['user_id'];
//        $refundLog['opposite_id'] = $data['opposite_id'];
//        $refundLog['money'] = $data['money'];
//        $refundLog['app_id'] = $data['app_id'];
//        $refundLog['borrow_log_id'] = $data['borrow_log_id'];
//        $refundLog['create_ip'] = $data['update_ip'];
//        $refundLog['update_ip'] = $data['update_ip'];
//        $refundLog['user_info'] = $data['user_info'];
//        $refundLog['opposite_info'] = $data['opposite_info'];
//        // 启动事务
//        $this->db()->startTrans();
//        try{
//            // 还款金额是否合理 <= 欠款金额 还款人金额是否充足
//            $findInfoUserAccount = $this->model($this->fundAccountModel)->lock(true)->where($whereFindUser)->find();
//            if(is_null($findInfoUserAccount)){ return \verify_err(null, '还款用户不存在或可用资金不足'); }
//            // 还款人的id 债主的id 等欠款记录信息是否对应
//            $findBorrowLog = $this->model($this->fundBorrowLogModel)->lock(true)->where($whereFindBorrowLog)->find();
//            if(is_null($findBorrowLog)){ return \verify_err(null, '没有符合条件的借款日志'); }
//            if($findBorrowLog['money'] - $findBorrowLog['refund_money'] < $data['money']){ return \verify_err(null, '还款金额大于欠款金额'); }
//            // 改变还款人,债主的资金
//            $resUpdateUserAccount = $this->model($this->fundAccountModel)->save($updateUserAccount, $whereUpdateUserAccount);
//            $resUpdateOppositeAccount = $this->model($this->fundAccountModel)->save($updateOppositeAccount, $whereUpdateOppositeAccount);
//            // 查询当前用户的资金状态
//            $infoUserAccount = $this->model($this->fundAccountModel)->lock(true)->where(['user_id'=>$data['user_id']])->find();
//            // 查询当前收款账户的资金状态
//            $infoOppositeAccount = $this->model($this->fundAccountModel)->lock(true)->where($whereUpdateOppositeAccount)->find();
//            // 补全用户资金日志
//            $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
//            // 补opposite资金日志
//            $saveOppositeLog = $this->_replenishFundLogInfo($saveOppositeLog, $infoOppositeAccount);
//            // 写入资金日志
//            $refundLog['fund_log_id'] = $this->_saveFundLogInfo($saveOppositeLog, $saveUserLog);
//            // 增加一条还款日志
//            $this->model($this->fundRefundLogModel)->save($refundLog);
//            // 将还款金额记录到借款日志表中
//            $this->model($this->fundBorrowLogModel)->save(['refund_money'=>['exp','refund_money+' . $data['money']]], ['id'=>$data['borrow_log_id']]);
//            // 验证是否还清所有款项,如果还清,将借款日志标记为已还清
//            $this->autoSetRefundStatus($data);
//            // 提交事务
//            $this->db()->commit();
//        } catch (\Exception $e) {
//            // 回滚事务
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//        return auto_return($infoUserAccount);
//    }
//    // 如果该借款已还清,则将某条借款日志标记为已还清
//    private function autoSetRefundStatus(array $data): array
//    {
//        $where['id'] = $data['borrow_log_id'];
//        $where['refund_money'] = ['exp', '>=money'];
//        $res = $this->model($this->fundBorrowLogModel)->save(['refund_status'=>'1'], $where);
//        return auto_return($res);
//    }

    # 冻结 a
    public function freeze(array $data): array
    {
        // 检测用户及可用金是否正常
        $whereFindUser = [['user_id','=',$data['user_id']],['cash','>=',$data['money']]];
        // 用户资金日志(部分)
        $userInfo = json_decode($data['user_info'], true);
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = $data['opposite_id'];
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'freeze';
        $saveUserLog['money'] = -$data['money'];
        $saveUserLog['remark'] = isset($userInfo['name']) ? '冻结用户' . $userInfo['name'] . '资金' : '冻结用户' . $data['user_id'] . '资金';
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = $data['user_info'];
        $saveUserLog['opposite_info'] = $data['opposite_info'];
        // 写入冻结记录表
        $freezeLog['user_id'] = $data['user_id'];
        $freezeLog['opposite_id'] = $data['opposite_id'];
        $freezeLog['app_id'] = $data['app_id'];
        $freezeLog['money'] = $data['money'];
        $freezeLog['unfreeze_money'] = 0;
        $freezeLog['create_ip'] = $data['update_ip'];
        $freezeLog['update_ip'] = $data['update_ip'];
        $freezeLog['freeze_status'] = 0;
        $freezeLog['user_info'] = $data['user_info'];
        $freezeLog['opposite_info'] = $data['opposite_info'];
        $freezeLog['type'] = $data['type'];
        $freezeLog['obj_id'] = $data['obj_id'];
        $freezeLog['freeze_counts'] = $data['freeze_counts'];
        // 启动事务
        DB::beginTransaction();
        try{
            // 检测用户及可用金是否正常
            $resFind = FundAccount::sharedLock()->where($whereFindUser)->first();
            if(!empty($resFind)){
                $resFind = $resFind->toArray();
            }
            if(is_null($resFind)){ return \verify_err(null, '没有符合条件的用户或用户可用金不足'); }
            // 冻结相应资金
            $resFreeze = DB::update("update `tp_fund_account` set `update_ip` = ?, `cash` = (cash-?), `freeze` = (freeze+?) where `user_id` = ?",[$data['update_ip'],$data['money'],$data['money'],$data['user_id']]);
            // 查询当前用户的资金状态
            $infoUserAccount = FundAccount::sharedLock()->where(['user_id'=>$data['user_id']])->first()->toArray();

            // 补全用户资金日志
            $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
            $freezeLog['fund_log_id'] = $this->_saveFundLogInfo($saveUserLog);
            // 写入冻结记录表
            $freezeLog['create_time'] = NOW;
            $freezeLog['update_time'] = NOW;
            FundfreezeLog::insert($freezeLog);
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollBack();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($data);
    }
    /*管理员冻结用户指定资金*/
    public function adminFreeze($data): array
    {
        //添加一条管理员操作原因记录表
        $data['create_ip'] = $data['update_ip'];
        $data['type'] = 'freeze_fund';
        unset($data['token']);
        $data['update_time'] = NOW;
        $data['create_time'] = NOW;
        $res = OperateReason::insertGetId($data);
        if(!$res){
            return verify_err(null, '添加操作原因记录失败');
        }
        $data['type'] = 'system';
        $data['obj_id'] = $res;
        $data['freeze_counts'] = 1;
        return $this->freeze($data);
    }
   # 解冻 a
    public function unfreeze(array $data): array
    {
        // 检测是否存在冻结日志及金额是否符合标准
        $whereFindUser = [['user_id','=',$data['user_id']],['freeze','>=',$data['money']]];
        $whereFreezeLog = [['type','=',$data['type']],['obj_id','=',$data['obj_id']],['freeze_counts','=',$data['freeze_counts']],['user_id','=',$data['user_id']],['status','=',1],['freeze_status','=',0]];
        $updateUserAccount['update_ip'] = $data['update_ip'];
        // 用户资金日志(部分)
        $userInfo = $data['user_info'];
        $oppositeInfo = $data['opposite_info'];
        $saveUserLog['user_id'] = $data['user_id'];
        $saveUserLog['opposite_id'] = $data['opposite_id'];
        $saveUserLog['app_id'] = $data['app_id'];
        $saveUserLog['type'] = 'unfreeze';
        $saveUserLog['money'] = $data['money'];
        $saveUserLog['remark'] = isset($userInfo['name']) ? '解冻用户' . $userInfo['name'] . '资金' : '解冻用户' . $data['user_id'] . '资金';
        $saveUserLog['create_ip'] = $data['update_ip'];
        $saveUserLog['update_ip'] = $data['update_ip'];
        $saveUserLog['status'] = 1;
        $saveUserLog['user_info'] = json_encode($data['user_info']);
        $saveUserLog['opposite_info'] = json_encode($data['opposite_info']);
        // 写入解冻记录表
        $unfreezeLog['user_id'] = $data['user_id'];
        $unfreezeLog['opposite_id'] = $data['opposite_id'];
        $unfreezeLog['app_id'] = $data['app_id'];
        $unfreezeLog['money'] = $data['money'];
        /*$unfreezeLog['freeze_log_id'] = $data['freeze_log_id'];*/
        $unfreezeLog['create_ip'] = $data['update_ip'];
        $unfreezeLog['update_ip'] = $data['update_ip'];
        $unfreezeLog['status'] = 1;
        $unfreezeLog['user_info'] = $data['user_info'];
        $unfreezeLog['opposite_info'] = $data['opposite_info'];
        $unfreezeLog['remark'] = $data['remark'] ?? '';
        // 更改冻结记录表
        // 将已全部解冻的记录表进行标记
        // 启动事务
        DB::beginTransaction();
        try{
            // 检测用户及可用金是否正常
            $resFind = FundAccount::sharedLock()->where($whereFindUser)->first();
            if(!empty($resFind)){
                $resFind = $resFind->toArray();
            }
            if(is_null($resFind)){ return \verify_err(null, '没有符合条件的用户或用户已冻结资金不足'); }
            // 检测冻结日志表
            $resFindFreezeLog = FundFreezeLog::where($whereFreezeLog)->first();
            if(is_null($resFindFreezeLog)){ return \verify_err(null, '没有找到相应的资金冻结记录'); }
            // 冻结相应资金
            $temp = FundAccount::where('user_id','=',$data['user_id'])->first();
            $updateUserAccount['cash'] = $temp['cash']+$data['money'];
            $updateUserAccount['freeze'] = $temp['freeze']-$data['money'];

            FundAccount::where('user_id','=',$data['user_id'])->update($updateUserAccount);
            // 查询当前用户的资金状态
            $infoUserAccount = FundAccount::sharedLock()->where('user_id','=',$data['user_id'])->first()->toArray();
            if($infoUserAccount['freeze'] < 0) { return \verify_err($infoUserAccount['freeze'], '解冻后的冻结金额小于0'); }
            // 补全用户资金日志
            $saveUserLog = $this->_replenishFundLogInfo($saveUserLog, $infoUserAccount);
            $unfreezeLog['fund_log_id'] = $this->_saveFundLogInfo($saveUserLog);
            $unfreezeLog['freeze_log_id'] = $resFindFreezeLog['id'];
            // 写入解冻记录表
            $unfreezeLog['create_time'] = NOW;
            $unfreezeLog['update_time'] = NOW;
            FundUnfreezeLog::insert($unfreezeLog);
            // 更改冻结记录表
            FundfreezeLog::where('id','=',$resFindFreezeLog['id'])->increment('unfreeze_money',$data['money']);
            // 将已全部解冻的记录表进行标记
            $this->_autoSetFreezeLogStatus(['freeze_log_id'=>$resFindFreezeLog['id']]);
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollBack();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($data);
    }
    // 设置已全部解冻的冻结日志表的状态
    private function _autoSetFreezeLogStatus(array $data): array
    {
        $where['id'] = $data['freeze_log_id'];
        $res = FundFreezeLog::where($where)->whereRaw('unfreeze_money >= money')->update(['freeze_status'=>'1']);
        return auto_return($res);
    }
    # 红包功能 a->b b(enter)
    // 补全用户资金日信息
    private function _replenishFundLogInfo(array $save, $info): array
    {
        $save['total'] = $info['total'];
        $save['income'] = $info['income'];
        $save['expend'] = $info['expend'];
        $save['cash'] = $info['cash'];
        $save['freeze'] = $info['freeze'];
        return $save;
    }
    // 补全用户资金日信息并存到相应数据表中,返回ID

    private function _saveFundLogInfo(...$userLogs)
    {
        $c=count($userLogs);
        for($i = 0; $i<$c; $i++){
            $userLogs[$i]['update_time'] = NOW;
            $userLogs[$i]['create_time'] = NOW;
        }

        FundOptionLog::insert($userLogs);
        return  DB::getPdo()->lastInsertId();
    }
    // 用户资金列表
    public function list(array $data): array
    {
        $where['status'] = 1;
        $query = FundAccount::where($where);
        if(!empty($data['keywords'])){
            $query->where('user_id', 'like', '%'.$data['keywords'].'%');
        }
        $res = $query->orderBy('id', 'desc')
            ->paginate($data['list_rows'], null, null, $data['page'])
            ->toArray();
        $res['data'] = $this->_addLotsUserInfo($res['data']);
        return auto_return_page($res, '已找到用户资金列表数据', '没有用户资金列表数据');
    }

    private function _addLotsUserInfo($data){
        $ids = [];
        foreach($data as $k=>$v){
            $ids[] = $v['user_id'];
        }
        $ids = implode(",",$ids);
        $arrayData = [];
        $userInfo = (new User())->infoByIds(['ids'=>$ids]);
        foreach($userInfo['res'] as $k=>$v){
            $arrayData[] = array_add($data[$k], 'user_info', $v);
        }
        return $arrayData;
    }
    // 某user_ids的用户资金列表
    public function listByUserIds(array $data): array
    {
        $where['status'] = 1;
        //$where['user_id'] = ['IN', $data['user_ids']];
        $res = FundAccount::where($where)->whereIn("user_id",[$data['user_ids']])->get();
        return auto_return($res, '已找到用户资金列表数据', '没有用户资金列表数据');
    }


    /**
     * 获取某用户的所有资产余额
     * @param $data
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function userFund($data): array
    {
        #
        ValidateFundAccount::checkValidate($data, ['user_id']);

        $res = [];
        $_where['user_id'] = $data['user_id'];
        $_where['status'] = 1;

        $fundInfo = FundAccount::where($_where)->first();
        $currencyInfo = CurrencyAccount::where($_where)->first();
        $integralInfo = IntegralAccount::where($_where)->first();

        $res['available_currency'] = $currencyInfo['available_currency'];
        $res['available_integral'] = $integralInfo['available_integral'];
        $res['freeze_currency'] = $currencyInfo['freeze'];
        $res['freeze_integral'] = $integralInfo['freeze'];
        $res['cash'] = $fundInfo['cash'];
        $res['freeze_cash'] = $fundInfo['freeze'];
        $res['account_status'] = $fundInfo['status'];
        $res['account_status_text'] = $fundInfo['status_text'];
        return auto_return($res, '成功获取用户资产余额', '获取用户资产余额失败');
    }
//
//    /*余额付款的订单进行确认收货操作*/
//    //解冻用户人民币/艺货币 + 用户转账平台人民币/艺货币
//    public function affirmPickUpGoods($data): array
//    {
//        $data['obj_id'] = $data['order_id'];
//        $CurrencyLogic = logic('\app\serverfund\logic\CurrencyAccount');
//        $_whereFreezeCounts['user_id'] = $data['user_id'];
//        $_whereFreezeCounts['type'] = $data['type'];
//        $_whereFreezeCounts['obj_id'] = $data['order_id'];
//        $_whereFreezeCounts['freeze_status'] = 0;
//        //开启事务
//        $this->db()->startTrans();
//        try{
//            if($data['pay_method'] == 'currency'){
//                //解冻用户艺货币
//                $data['currency'] = $data['certificate_price'];
//                $data['freeze_counts'] = $this->model($this->currencyFreezeLogModel)->where($_whereFreezeCounts)->min('freeze_counts');
//                $unfreezeRes = $CurrencyLogic->unfreeze($data);
//                if($unfreezeRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $unfreezeRes;
//                }
//                //用户转账平台艺货币
//                $transferRes = $CurrencyLogic->transfer($data);
//                if($transferRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $transferRes;
//                }
//            }else{
//                //解冻用户人民币
//                $data['money'] = $data['certificate_price'];
//                $data['freeze_counts'] = $this->model($this->fundFreezeLogModel)->where($_whereFreezeCounts)->min('freeze_counts');
//                $unfreezeRes = $this->unfreeze($data);
//                if($unfreezeRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $unfreezeRes;
//                }
//                //用户转账平台人民币
//                $transferRes = $this->transfer($data);
//                if($transferRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $transferRes;
//                }
//            }
//            $this->db()->commit();
//        }catch(\Exception $e){
//            $this->db()->rollback();
//            return server_err(null, '数据库异常:' . $e->getMessage());
//        }
//        return auto_success($data, '成功进行资金操作');
//    }
//
//    /*我的证书 - 退款*/
//    //流程：
//    //1、根据证书表中的付款方式pay_method解冻证书表中的certificate_price用户资金(从小到大解冻)
//    //2、扣除用户积分个数（用户转账平台积分-累计收益积分）
//    //3、扣除用户亚数币个数（用户转账平台亚数币-证书转让费用）
//    public function sendBack($data): array
//    {
//        $_whereFreezeCounts['user_id'] = $data['user_id'];
//        // $_whereFreezeCounts['app_id'] = $data['app_id'];
//        $_whereFreezeCounts['type'] = 'order';
//        $_whereFreezeCounts['obj_id'] = $data['order_id'];
//        $_whereFreezeCounts['freeze_status'] = 0;
//        $IntegralLogic = logic('\app\serverfund\logic\IntegralAccount');
//        $CurrencyLogic = logic('\app\serverfund\logic\CurrencyAccount');
//        //开启事务
//        $this->db()->startTrans();
//        try{
//            //1、解冻用户资金
//            $data['type'] = 'order';
//            $data['obj_id'] = $data['order_id'];
//            if($data['pay_method'] == 'currency'){  //艺货币
//                //获取冻结次数最小的一条记录
//                $data['freeze_counts'] = $this->model($this->currencyFreezeLogModel)->where($_whereFreezeCounts)->min('freeze_counts');
//                $data['currency'] = $data['certificate_price'];
//                $unfreezeCurrency = $CurrencyLogic->unfreeze($data);
//                if($unfreezeCurrency['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $unfreezeCurrency;
//                }
//            }else{  //人民币
//                $data['freeze_counts'] = $this->model($this->fundFreezeLogModel)->where($_whereFreezeCounts)->min('freeze_counts');
//                $data['money'] = $data['certificate_price'];
//                $unfreezeFund = $this->unfreeze($data);
//                if($unfreezeFund['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $unfreezeFund;
//                }
//            }
//            /*2、扣除用户亚数币个数（用户转账平台亚数币-证书转让费用）*/
//            if($data['certi_transfer_fee']){
//                $data['currency'] = $data['certi_transfer_fee'];
//                $transferRes = $CurrencyLogic->deduct($data);
//                if($transferRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $transferRes;
//                }
//            }
//            $this->db()->commit();
//        }catch(\Exception $e){
//            $this->db()->rollback();
//            return server_err(null, '数据库异常' . $e->getMessage());
//        }
//        return auto_success($data, '成功进行退款');
//    }
//
//    // 同意证书转让或购买证书时的资金操作
//    // 1、解冻卖家手续费
//    // 2、扣除卖家手续费
//    // 3、买家转让卖家证书价格(若使用了积分抵扣，先进行积分化成钱的)
//    public function buyCertificate($data): array
//    {
//        // 解冻卖家手续费  A用户对证书1改价 只存在一条未解冻的日志
//        $_whereFindFreeze['user_id'] = $data['seller_id'];
//        $_whereFindFreeze['type'] = 'certi_change_price';
//        $_whereFindFreeze['obj_id'] = $data['certificate_id'];
//        $_whereFindFreeze['money'] = $data['procedure_cost'];
//        $_whereFindFreeze['freeze_status'] = 0;
//        $_whereFindFreeze['status'] = 1;
//        $freezeList = $this->model($this->fundFreezeLogModel)->where($_whereFindFreeze)->select();
//        if(count($freezeList) != 1){
//            return verify_err(null, '不存在或存在多于一条的该证书的冻结手续费记录');
//        }
//        $unfreeze['user_id'] = $data['seller_id'];
//        $unfreeze['opposite_id'] = $data['system_id'];
//        $unfreeze['type'] = 'certi_change_price';
//        $unfreeze['obj_id'] = $data['certificate_id'];
//        $unfreeze['freeze_counts'] = $freezeList[0]['freeze_counts'];
//        $unfreeze['money'] = $data['procedure_cost'];
//        $unfreeze['update_ip'] = $data['update_ip'];
//        $unfreeze['app_id'] = $data['app_id'];
//        $unfreeze['user_info'] = $data['seller_info'];
//        $unfreeze['opposite_info'] = $data['system_info'];
//        // 开启事务
//        $this->db()->startTrans();
//        try{
//            $unfreezeRes = $this->unfreeze($unfreeze);
//            if($unfreezeRes['status'] != SUCCESS_STATUS){
//                $this->db()->rollback();
//                return $unfreezeRes;
//            }
//            // 扣除卖家手续费 即卖家转账平台
//            $transfer['user_id'] = $data['seller_id'];
//            $transfer['opposite_id'] = $data['system_id'];
//            $transfer['money'] = $data['procedure_cost'];
//            $transfer['update_ip'] = $data['update_ip'];
//            $transfer['app_id'] = $data['app_id'];
//            $transfer['user_info'] = $data['seller_info'];
//            $transfer['opposite_info'] = $data['system_info'];
//            $transferRes = $this->transfer($transfer);
//            if($transferRes['status'] != SUCCESS_STATUS){
//                $this->db()->rollback();
//                return $transferRes;
//            }
//            // 如果使用了积分抵扣，验证抵扣信息是否正确
//            if($data['is_use_integral'] == 1){
//                // 获取证书的支付信息
//                $userFund = $this->userFund(['user_id'=>$data['buyer_id']]);
//                $getPayInfo['user_available_integral'] = $userFund['res']['available_integral'];
//                $getPayInfo['certi_max_integral_deduction'] = $data['certi_max_integral_deduction'];
//                $getPayInfo['certi_total_price'] = $data['sell_price'];
//                $getPayInfo['app_id'] = $data['app_id'];
//                $payInfoRes = $this->logic($this->integralAccountLogic)->getCertiPayInfo($getPayInfo);
//                if($payInfoRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $payInfoRes;
//                }
//                if($payInfoRes['res']['integral_deduction'] != $data['integral_deduction']){
//                    $this->db()->rollback();
//                    return verify_err(null, '积分抵扣个数错误');
//                }
//                if($payInfoRes['res']['integral_deduction_fund'] != $data['integral_deduction_fund']){
//                    $this->db()->rollback();
//                    return verify_err(null, '积分抵扣资金错误');
//                }
//                // 扣除买家积分个数（买家转账平台积分）：integral_deduction
//                $transferIntegral['user_id'] = $data['buyer_id'];
//                $transferIntegral['opposite_id'] = $data['system_id'];
//                $transferIntegral['integral'] = $data['integral_deduction'];
//                $transferIntegral['update_ip'] = $data['update_ip'];
//                $transferIntegral['app_id'] = $data['app_id'];
//                $transferIntegral['user_info'] = $data['buyer_info'];
//                $transferIntegral['opposite_info'] = $data['system_info'];
//                $transferIntegralRes = $this->logic($this->integralAccountLogic)->transfer($transferIntegral);
//                if($transferIntegralRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $transferIntegralRes;
//                }
//                // 给买家充值余额：integral_deduction_fund
//                $recharge['user_id'] = $data['buyer_id'];
//                $recharge['money'] = $data['integral_deduction_fund'];
//                $recharge['update_ip'] = $data['update_ip'];
//                $recharge['app_id'] = $data['app_id'];
//                $recharge['user_info'] = $data['buyer_info'];
//                $recharge['opposite_info'] = $data['system_info'];
//                $rechargeRes = $this->prepaid($recharge);
//                if($rechargeRes['status'] != SUCCESS_STATUS){
//                    $this->db()->rollback();
//                    return $rechargeRes;
//                }
//            }
//            // 买家转帐卖家证书价格
//            $transferToSeller['user_id'] = $data['buyer_id'];
//            $transferToSeller['opposite_id'] = $data['seller_id'];
//            $transferToSeller['money'] = $data['sell_price'];
//            $transferToSeller['update_ip'] = $data['update_ip'];
//            $transferToSeller['app_id'] = $data['app_id'];
//            $transferToSeller['user_info'] = $data['buyer_info'];
//            $transferToSeller['opposite_info'] = $data['seller_info'];
//            $transferToSellerRes = $this->transfer($transferToSeller);
//            if($transferToSellerRes['status'] != SUCCESS_STATUS){
//                $this->db()->rollback();
//                return $transferToSellerRes;
//            }
//            $this->db()->commit();
//        }catch(\Exception $e){
//            $this->db()->rollback();
//            return server_err(null, '数据库异常' . $e->getMessage());
//        }
//        return auto_success($data, '成功完成购买证书的资金流程');
//    }
//
//    // 证书改价时资金的操作
//    // 是否有上一次卖家A改价未解冻的（有且仅有一个） 有则进行解冻
//    // 冻结这一次的手续费
//    public function certiChangePrice($data): array
//    {
//        // 判断是否为第一次修改价格
//        $_where['user_id'] = $data['user_id'];
//        $_where['type'] = 'certi_change_price';
//        $_where['obj_id'] = $data['certificate_id'];
//        $_where['status'] = 1;
//        $_where['money'] = $data['old_procedure_price'];
//        $_where['freeze_status'] = 0;
//        $unfreezeList = $this->model($this->fundFreezeLogModel)->where($_where)->select();
//        if(count($unfreezeList) > 1){
//            return verify_err(null, '该证书因改价有不止一笔未解冻资金');
//        }
//        if($unfreezeList && count($unfreezeList) == 1){  // 解冻上一次
//            $unfreezeOld['user_id'] = $data['user_id'];
//            $unfreezeOld['opposite_id'] = $data['opposite_id'];
//            $unfreezeOld['type'] = 'certi_change_price';
//            $unfreezeOld['obj_id'] = $data['certificate_id'];
//            $unfreezeOld['freeze_counts'] = $unfreezeList[0]['freeze_counts'];
//            $unfreezeOld['money'] = $data['old_procedure_price'];
//            $unfreezeOld['update_ip'] = $unfreezeList[0]['update_ip'];
//            $unfreezeOld['app_id'] = $unfreezeList[0]['app_id'];
//            $unfreezeOld['user_info'] = $data['user_info'];
//            $unfreezeOld['opposite_info'] = $data['opposite_info'];
//            $unfreezeOldRes = $this->unfreeze($unfreezeOld);
//            if($unfreezeOldRes['status'] != SUCCESS_STATUS){
//                return $unfreezeOldRes;
//            }
//        }
//        // 获取冻结时的次数
//        $_getCountWhere['user_id'] = $data['user_id'];
//        $_getCountWhere['type'] = 'certi_change_price';
//        $_getCountWhere['obj_id'] = $data['certificate_id'];
//        $_getCountWhere['status'] = 1;
//        $counts = $this->model($this->fundFreezeLogModel)->where($_getCountWhere)->order('id desc')->value('freeze_counts');
//        // 冻结这一次
//        $freezeNew['user_id'] = $data['user_id'];
//        $freezeNew['opposite_id'] = $data['opposite_id'];
//        $freezeNew['money'] = $data['new_procedure_price'];
//        $freezeNew['update_ip'] = $data['update_ip'];
//        $freezeNew['app_id'] = $data['app_id'];
//        $freezeNew['user_info'] = $data['user_info'];
//        $freezeNew['opposite_info'] = $data['opposite_info'];
//        $freezeNew['type'] = 'certi_change_price';
//        $freezeNew['obj_id'] = $data['certificate_id'];
//        $freezeNew['freeze_counts'] = $counts ? $counts + 1 : 1;
//        return $this->freeze($freezeNew);
//    }
//
//    // 解冻证书持有人因改价所冻结的手续费
//    public function unfreezeProcedure($data): array
//    {
//        $_where['user_id'] = $data['user_id'];
//        $_where['type'] = 'certi_change_price';
//        $_where['obj_id'] = $data['certificate_id'];
//        $_where['status'] = 1;
//        $_where['money'] = $data['procedure_price'];
//        $_where['freeze_status'] = 0;
//        $unfreezeList = $this->model($this->fundFreezeLogModel)->where($_where)->select();
//        if(count($unfreezeList) != 1){
//            return verify_err(null, '该证书因改价应有且只有一条未解冻资金记录');
//        }
//        $unfreezeOld['user_id'] = $data['user_id'];
//        $unfreezeOld['opposite_id'] = $data['opposite_id'];
//        $unfreezeOld['type'] = 'certi_change_price';
//        $unfreezeOld['obj_id'] = $data['certificate_id'];
//        $unfreezeOld['freeze_counts'] = $unfreezeList[0]['freeze_counts'];
//        $unfreezeOld['money'] = $data['procedure_price'];
//        $unfreezeOld['update_ip'] = $unfreezeList[0]['update_ip'];
//        $unfreezeOld['app_id'] = $unfreezeList[0]['app_id'];
//        $unfreezeOld['user_info'] = $data['user_info'];
//        $unfreezeOld['opposite_info'] = $data['opposite_info'];
//        return $this->unfreeze($unfreezeOld);
//    }
}